package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybb.vue.common.R;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.ExcelUtils;
import com.ybb.vue.domain.GoodsInStatusEnum;
import com.ybb.vue.domain.GoodsInTypeEnum;
import com.ybb.vue.domain.GoodsOutStatusEnum;
import com.ybb.vue.domain.GoodsTypeEnum;
import com.ybb.vue.domain.dto.GoodsInAddDto;
import com.ybb.vue.domain.dto.GoodsInByNameAndTypeDto;
import com.ybb.vue.domain.dto.GoodsOutAddDto;
import com.ybb.vue.domain.vo.EpmResidentExportVo;
import com.ybb.vue.domain.vo.GoodsOutExportVo;
import com.ybb.vue.entity.Approval;
import com.ybb.vue.entity.GoodsIn;
import com.ybb.vue.entity.GoodsInventory;
import com.ybb.vue.entity.GoodsOut;
import com.ybb.vue.mapper.GoodsOutMapper;
import com.ybb.vue.service.ApprovalService;
import com.ybb.vue.service.GoodsOutService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description TODO
 * @Date 2023/4/4 21:48
 */

@RestController
@RequestMapping("/goodsOut")
public class GoodsOutController {

    @Autowired
    private GoodsOutService goodsOutService;

    @Autowired
    private GoodsOutMapper goodsOutMapper;

    @Autowired
    private ApprovalService approvalService;

    /**
     * 物资出库列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/goodsOutList")
    @PreAuthorize("hasAuthority('epidemic:goods:outlist')"+"||"+"hasAuthority('epidemic:goods:outlquery')")
    public Result<?> goodsInList(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<GoodsOut> queryWrapper = Wrappers.<GoodsOut>lambdaQuery();
        queryWrapper.like(GoodsOut::getGoodsName, search);
        queryWrapper.orderByDesc(GoodsOut::getOutTime);
        Page<GoodsOut> result = goodsOutService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<GoodsOut> goodsOutList = result.getRecords();
        goodsOutList.forEach(goodsOut -> {
            // 入库状态转译
            goodsOut.setOutStatus(GoodsOutStatusEnum.getGoodsOutStatus(goodsOut.getOutStatus()));
            // 物资类别转译
            goodsOut.setGoodsType(GoodsTypeEnum.getGoodsType(goodsOut.getGoodsType()));
        });
        return Result.success(result);
    }

    /**
     * 导出
     *
     * @param response
     * @param search
     */
    @GetMapping("/export")
    @PreAuthorize("hasAuthority('epidemic:goods:outlexport')")
    public void exportResident(HttpServletResponse response, @RequestParam(defaultValue = "") String search) {
        List<GoodsOut> goodsOutList = goodsOutMapper.selectList(new QueryWrapper<GoodsOut>().lambda().like(GoodsOut::getGoodsName, search));
        List<GoodsOutExportVo> exportList = new ArrayList<>();
        goodsOutList.forEach(goodsOut -> {
            GoodsOutExportVo goodsOutExportVo = new GoodsOutExportVo();
            BeanUtils.copyProperties(goodsOut, goodsOutExportVo);
            // 入库状态转译
            goodsOutExportVo.setOutStatus(GoodsOutStatusEnum.getGoodsOutStatus(goodsOutExportVo.getOutStatus()));
            // 物资类别转译
            goodsOutExportVo.setGoodsType(GoodsTypeEnum.getGoodsType(goodsOutExportVo.getGoodsType()));
            exportList.add(goodsOutExportVo);
        });
        ExcelUtils.export(response, "出库单信息表", exportList, EpmResidentExportVo.class);

    }

    /**
     * 新增资产出库
     *
     * @param goodsOutAddDto
     * @return
     */
    @PostMapping("/out")
    @PreAuthorize("hasAuthority('epidemic:goods:out')")
    @Transactional
    public Result<?> goodInAdd(@RequestBody @Valid GoodsOutAddDto goodsOutAddDto) {
        return goodsOutService.out(goodsOutAddDto);
    }

    /**
     * 物资清单要用，提供记录
     *
     * @param dto
     * @return
     */
    @PostMapping("/goodsOutNotesByName")
    @PreAuthorize("hasAuthority('epidemic:goods:detail')")
    public Result<?> goodsOutNotesByName(@RequestBody @Valid GoodsInByNameAndTypeDto dto) {
        LambdaQueryWrapper<GoodsOut> queryWrapper = Wrappers.<GoodsOut>lambdaQuery();
        queryWrapper.eq(GoodsOut::getGoodsName, dto.getGoodsName());
        queryWrapper.eq(GoodsOut::getGoodsType, dto.getGoodsType());
        queryWrapper.eq(GoodsOut::getOutStatus, "1");
        queryWrapper.orderByDesc(GoodsOut::getOutTime);
        List<GoodsOut> goodsOutList = goodsOutMapper.selectList(queryWrapper);
        goodsOutList.forEach(goodsOut -> {
            // 入库状态转译
            goodsOut.setOutStatus(GoodsOutStatusEnum.getGoodsOutStatus(goodsOut.getOutStatus()));
            // 物资类别转译
            goodsOut.setGoodsType(GoodsTypeEnum.getGoodsType(goodsOut.getGoodsType()));
        });
        return Result.success(goodsOutList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping()
    @Transactional
    @PreAuthorize("hasAuthority('epidemic:goods:outdelete')")
    public R delete(Long id) {
        approvalService.remove(new QueryWrapper<Approval>().lambda().eq(Approval::getSubjectId, id).eq(Approval::getSubjectSource, "OUT"));
        goodsOutService.removeById(id);
        return R.ok();
    }

    /**
     * 物资出库列表 不分页，详情使用
     *
     * @return
     */
    @GetMapping("/goodsOutDetail")
    @PreAuthorize("hasAuthority('epidemic:goods:outdetail')")
    public Result<?> goodsOutDetail(Long id) {
        GoodsOut goodsOut = goodsOutMapper.selectById(id);

        // 入库状态转译
        goodsOut.setOutStatus(GoodsOutStatusEnum.getGoodsOutStatus(goodsOut.getOutStatus()));
        // 物资类别转译
        goodsOut.setGoodsType(GoodsTypeEnum.getGoodsType(goodsOut.getGoodsType()));

        return Result.success(goodsOut);
    }


}
